package cn.com.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.com.common.aop.annotation.LogAnnotation;
import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gt3.service.DmGySwjgService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门管理
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@RequestMapping("/sys")
@RestController
@Api(tags = "组织模块-机构管理")
public class DeptController {
	@Resource
	private DeptService deptService;
	
	@Resource
	private DmGySwjgService dmGySwjgService;
	
	@Resource
	private  HttpSessionService httpSessionService;
	
	@PostMapping("/dept")
	@ApiOperation(value = "新增组织接口")
	@LogAnnotation(title = "机构管理", action = "新增组织")
	@RequiresPermissions("sys:dept:add")
	public DataResult addDept(@RequestBody @Valid SysDept vo) {
		deptService.addDept(vo);
		return DataResult.success();
	}

	@DeleteMapping("/dept/{id}")
	@ApiOperation(value = "删除组织接口")
	@LogAnnotation(title = "机构管理", action = "删除组织")
	@RequiresPermissions("sys:dept:deleted")
	public DataResult deleted(@PathVariable("id") String id) {
		deptService.deleted(id);
		return DataResult.success();
	}

	@PutMapping("/dept")
	@ApiOperation(value = "更新组织信息接口")
	@LogAnnotation(title = "机构管理", action = "更新组织信息")
	@RequiresPermissions("sys:dept:update")
	public DataResult updateDept(@RequestBody SysDept vo) {
		if (StringUtils.isEmpty(vo.getId())) {
			return DataResult.fail("id不能为空");
		}
		deptService.updateDept(vo);
		return DataResult.success();
	}

	@GetMapping("/dept/{id}")
	@ApiOperation(value = "查询组织详情接口")
	@LogAnnotation(title = "机构管理", action = "查询组织详情")
	@RequiresPermissions("sys:dept:detail")
	public DataResult detailInfo(@PathVariable("id") String id) {
		return DataResult.success(deptService.getById(id));
	}

	@GetMapping("/dept/tree")
	@ApiOperation(value = "树型组织列表接口")
	@LogAnnotation(title = "机构管理", action = "树型组织列表")
	@RequiresPermissions(value = { "sys:user:list", "sys:user:update", "sys:user:add", "sys:dept:add",
			"sys:dept:update" }, logical = Logical.OR)
	public DataResult getTree(@RequestParam(required = false) String deptId) {
		return DataResult.success(deptService.deptTreeList(deptId));
		// List<DeptRespNodeVO> deptTreeList = dmGySwjgService.deptTreeList(deptId);
		// return DataResult.success(deptTreeList);
	}
	
	
	
	
	
	@GetMapping("/depts")
	@ApiOperation(value = "获取机构列表接口")
	@LogAnnotation(title = "机构管理", action = "获取所有组织机构")
	@RequiresPermissions("sys:dept:list")
	public DataResult getDeptAll() {
		
		String currentDeptId = httpSessionService.getCurrentDeptId();
		// String sjswjgdm = dmGySwjgService.getById(currentDeptId).getSjswjgDm();
		
		String taxOrgLevel = TsBqUtils.getTaxOrgLevel(currentDeptId);
		QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();

		if(taxOrgLevel.equals(Constants.TAX_PROVINCES)){
			// 省局
		}else if(taxOrgLevel.equals(Constants.TAX_CITIES)){
			// 市局
			queryWrapper.likeRight("id", currentDeptId.substring(0,5));
		}else if(taxOrgLevel.equals(Constants.TAX_COUNTES)){
			// 县区局
			queryWrapper.likeRight("id", currentDeptId.substring(0,7));
		}else{
			return DataResult.fail("税务机关层级获取存在问题！");
		}
		
		queryWrapper.eq("deleted",Constants.DELETEED_NOT);
		
		List<SysDept> entityList = deptService.list(queryWrapper);
		
		
		entityList.parallelStream().forEach(entity -> {
			
			String id = entity.getId();
			if(taxOrgLevel.equals(Constants.TAX_CITIES)){
				// 市局
				if((currentDeptId.substring(0,5) + "000000").equals(id)){
					entity.setPid("0");
				};
				
			}else if(taxOrgLevel.equals(Constants.TAX_COUNTES)){
				// 县区局
				if((currentDeptId.substring(0,7) + "0000").equals(id)){
					entity.setPid("0");
				};
				
			}
			
			SysDept parentEntity = deptService.getById(entity.getPid());
			if (parentEntity != null) {
				entity.setPidName(parentEntity.getName());
			}
		});
		
		return DataResult.success(entityList);
	}
	
	
	
	@GetMapping("/deptlist")
	@ApiOperation(value = "获取机构列表接口")
	@LogAnnotation(title = "机构管理", action = "获取所有组织机构")
	public DataResult deptList() {
		String currentDeptId = httpSessionService.getCurrentDeptId();
		List<Object> list = new ArrayList<Object>();
		QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
		
		if(Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))){
			// 县区局
			queryWrapper.eq("id", currentDeptId);
			
		}else if(Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))){
			// 市局 
			queryWrapper.like("id", currentDeptId.substring(0,5));
		
		}
		
		
		queryWrapper.eq("deleted", Constants.DATA_NOT_DELETED);
		list.add(deptService.listMaps(queryWrapper));
		return DataResult.success(list);

	}
	
	
}
